<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Wb_share_model extends MY_Model {
    
    protected $TB_WB_SHARE = 'dc_share';
    protected $TB_WB_USER = 'dc_user';
    protected $WB_SHARE_COLS = array(
        'share_id',
        'user_id',
        'product_id',
        'share_content',
        'share_post_id',
        'share_create_time',
        'share_is_display'
    );
    
    public function __construct() {
        parent::__construct();
    }

    public function add_share($user_id, $product_id, $share_content, $share_post_id, $share_create_time=null) {
        $share_create_time = $share_create_time or date('Y-m-d H:i:s', time());
        $query = $this->db->query("INSERT INTO $this->TB_WB_SHARE 
            (`user_id`, `product_id`, `share_content`, `share_post_id`, `share_create_time`) VALUES
            ('$user_id', '$product_id', '$share_content', '$share_post_id', '$share_create_time')");
        $share_id = $query ? $this->db->insert_id() : false;
        return $share_id;
    }

	public function has_share($user_id, $product_id){
		$query = $this->db->query("SELECT `share_id` FROM $this->TB_WB_SHARE
            WHERE `user_id` = $user_id AND `product_id` = $product_id  LIMIT 1");
		if($query->num_rows()) return true;
		return false;
	}
    
    public function del_share($share_id) {
        return $this->db->query("DELETE FROM $this->TB_WB_SHARE
            WHERE `share_id` = $share_id LIMIT 1");
    }
    
    public function set_share($share_id, $sets) {
        $sets_sql = $this->_sets_to_str($sets, $this->WB_SHARE_COLS);
        return $this->db->query("UPDATE $this->TB_WB_SHARE SET $sets_sql 
            WHERE `share_id` = $share_id");
    }
    
    public function get_product_share_list($product_id, $start_index, $step, $cols=null) {
		$start_index--;
        return $this->db->query("SELECT S.share_id, U.user_wb_screen_name, S.product_id, S.share_content, S.share_create_time FROM $this->TB_WB_SHARE S, $this->TB_WB_USER U 
		WHERE S.user_id = U.user_id and S.product_id = $product_id ORDER BY S.share_id desc LIMIT $start_index, $step")
				->result();
    }
    
    public function get_display_share_list($cols=null) {
        return $this->db->query("SELECT `share_id`, U.`user_wb_screen_name`, `product_id`,
		`share_content` FROM $this->TB_WB_SHARE S,
		$this->TB_WB_USER U WHERE S.`user_id` = U.`user_id` and S.share_is_display = 1 ORDER BY `product_id`,
		`share_id` desc")
				->result();
    }

	public function get_sum_production_share($product_id){
		$query = $this->db->query("SELECT `share_id` FROM $this->TB_WB_SHARE
            WHERE `product_id` = $product_id");
		return $query->num_rows();
	}
    
    public function get_product_display_share_list($product_id, $cols=null) {
        $cols_str = $cols ? join(',', $cols) : join(',', $this->WB_SHARE_COLS);
        return $this->db->query("SELECT S.share_id, U.user_wb_screen_name, S.product_id,
		S.share_content FROM $this->TB_WB_SHARE S, $this->TB_WB_USER U WHERE 
		S.user_id = U.user_id and S.product_id = $product_id and S.share_is_display = 1")
				->result();
    }
}